package day20180131.designMode.twelve_Bridge;

/**
 * Created by 14394 on 2018/1/31.
 */
//目标接口
public interface Source {
    void method();
}

//目标实现一
class SouceImp1 implements Source{
    @Override
    public void method() {
        System.out.println("SourceImp1 method run");
    }
}

//目标实现二
class SouceImp2 implements Source{
    @Override
    public void method() {
        System.out.println("SourceImp2 method run");
    }
}

//桥抽象
abstract class Bridge implements Source{
    private Source source;

    @Override
    public void method() {
        source.method();
    }

    public Source getSource() {
        return source;
    }

    public void setSource(Source source) {
        this.source = source;
    }
}

//自定义的桥
class  MyBridge extends Bridge{
    @Override
    public void method() {
        getSource().method();
    }


    /*使用场景：   不希望或者不适合使用继承的场景
    *             接口或者抽象类不稳定的场景
    *             重要性要求较高的场景
    *  */

    public static void main(String[] args) {
        Bridge bridge = new MyBridge();

        Source source1 = new SouceImp1();
        bridge.setSource(source1);
        bridge.method();

        Source source2 = new SouceImp2();
        bridge.setSource(source2);
        bridge.method();
    }
}